﻿using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using Serilog;
using xjjxmm.infrastructure.Authorizations;
using XjjXmm.Infrastructure.Common;
using xjjxmm.infrastructure.webs.filter;
using XjjXmm.Infrastructure.Configuration;
using XjjXmm.Infrastructure.Constant;
using XjjXmm.Infrastructure.Jwt;
using XjjXmm.Infrastructure.Webs.Route;
using Microsoft.AspNetCore.Mvc;
using xjjxmm.infrastructure.Common;

namespace xjjxmm.infrastructure.compont;

public class ControllerCompont : ICompont
{
    public int Order { get; set; } = 100;

    public bool Regist(WebApplicationBuilder builder) {
        var frameConfig = ConfigHelper.GetSection<FrameworkConfig>(CommonConstant.FrameworkConfigKey);
        //用newtionsoftjson 代替默认方案， 
        builder.Services.AddControllers(opt =>
        {            // 路由参数在此处仍然是有效的，比如添加一个版本号
            if (!frameConfig?.Prefix?.IsNullOrEmpty() ?? false)
            {
                opt.UseCentralRoutePrefix(new RouteAttribute(frameConfig!.Prefix));
            }
        })
            .AddMvcOptions(t =>
            {

                if (frameConfig?.UsePrettyResult == "1")
                {
                    t.Filters.Add<MvcResultFilter>();
                }

                t.Filters.Add<MvcExceptionFilter>();


                //t.Filters.Add(new AuthorizeFilter());
            }).AddNewtonsoftJson(option =>
            {
                option.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                option.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                option.SerializerSettings.Converters.Add(new LongJsonConverter());
                //option.ContractResolver = new CamelCasePropertyNamesContractResolver();


            }).AddControllersAsServices();

        return false;
    }

   
}
